package com.tcode.consumer.controller;







import com.tcode.consumer.client.ExposeClient;
import com.tcode.provider.entity.Info;
import com.tcode.provider.entity.User;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/consumers")
public class ConsumerController {
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Autowired
//    private DiscoveryClient client;

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerController.class);

    @Autowired
    private ExposeClient client;

    @GetMapping("/login")
    public String login(String uname, String upassword) {
        System.out.println();
        System.out.println("消费者 控制层");
        System.out.println(uname);
//restTemplate访问生产者
//        List<ServiceInstance> list = client.getInstances("provider");
//        ServiceInstance instance = list.get(0);
//        System.out.println(instance.getHost()+":"+instance.getPort());
//        String infos = restTemplate.postForObject("http://localhost:"+instance.getPort()+"/users/login", user,String.class);
        //String infos = restTemplate.postForObject("http://provider/users/login", user,String.class);
//        System.out.println(infos);
        String infos = client.login(uname, upassword);
        return infos;
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        System.out.println(file);
        if (file.isEmpty()) {

            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "D:/upload/";
        System.out.println("consumer提交的文件路径filePath："+filePath);
        System.out.println("consumer提交的文件路径filePath："+filePath+fileName);
        String filePath1=filePath+fileName;


        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            client.upload(filePath1);
            return "上传成功";

        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败！";
    }
    public void test(){
        System.out.println("比较版本差异");
    }

}
